set more off

use dataset, clear

local nreps 500
 
local Z pcteligible2013
su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

gen B = .
gen UB = .
gen LB = .
gen N = .

egen senexpand2014 = mean(Sen2014) if z == 1, by(border)
egen sennoexpand2014 = mean(Sen2014) if z == 0, by(border)
egen gubexpand2014 = mean(Gub2014) if z == 1, by(border)
egen gubnoexpand2014 = mean(Gub2014) if z == 0, by(border)

egen senexpand2010 = mean(Sen2010) if z == 1, by(border)
egen sennoexpand2010 = mean(Sen2010) if z == 0, by(border)
egen gubexpand2010 = mean(Gub2010) if z == 1, by(border)
egen gubnoexpand2010 = mean(Gub2010) if z == 0, by(border)

levelsof border, local(levels)

foreach b of local levels {
	reg dregistration20142010 z x zx if border == "`b'", robust
	replace N = e(N) if border == "`b'"
	replace B = _b[z] if border == "`b'"
	replace UB = _b[z] + 1.96*_se[z] if border == "`b'"
	replace LB = _b[z] - 1.96*_se[z] if border == "`b'"
}

collapse N B UB LB sennoexpand* gubnoexpand* senexpand* gubexpand*, by(border)

gen racesexpand = string(senexpand2010) + string(senexpand2014) + string(gubexpand2010) + string(gubexpand2014)
gen racesnoexpand = string(sennoexpand2010) + string(sennoexpand2014) + string(gubnoexpand2010) + string(gubnoexpand2014)

drop sen* gub*

drop if B == 0 & UB == 0 & LB == 0

gen same = racesexpand == racesnoexpand
label define samel 1 "Same races" 0 "Different races", replace
label values same samel

sort same B

bys same: gen index = _n

#delimit;

gr tw 
	(rspike UB LB index, col(gs10))
	(sc B index, col(black) mlab(border) mlabangle(270) msym(O))
	,
		by(same, legend(off) xrescale note(""))
		xlab(none)
		ylab(-10(5)10)
		plotregion(style(none))
		yline(0, lpat(dash))
		subtitle(, fcol(none) lwid(0) lcol(none))
		ylab(, angle(horiz) nogrid)
		xtitle("")
		;		

#delimit cr

gr export "_output/figureA13.pdf", replace
gr close
